import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';

const app = getApp();

Page({
    data: {
        member: 0
    },
    async onReady() {
        const db = qq.cloud.database();
        const _ = db.command;
        let This = this;
        await db.collection("black")
            .where({
                alliance: app.data.alliance
            })
            .get()
            .then(res => {
                This.setData({ member: res.data });
            });
    },
    onShareAppMessage() {
        qq.showShareMenu({
            showShareItems: ['qq', 'qzone', 'wechatFriends', 'wechatMoment']
        });
    },
    async recover(e) {
        await Dialog.confirm({
                title: '提示',
                message: '请确认恢复该成员信息',
            })
            .then(async() => {
                const db = qq.cloud.database();
                const _ = db.command;
                let admin = false;
                await db.collection('administer').where({
                    unionId: _.eq(app.data.unionId)
                }).get().then((res) => {
                    if (res.data.length != 0) {
                        admin = true
                    }
                })
                if (admin) {
                    db.collection('black').where({
                        userName: e.target.dataset.username,
                        unionId: e.target.dataset.id,
                        alliance: app.data.alliance
                    }).remove().then(res => {
                        if (res.deleted != 0) {
                            for (let i in this.data.member) {
                                if (this.data.member[i].userName == e.target.dataset.username && this.data.member[i].unionId == e.target.dataset.id) {
                                    this.data.member.splice(i, 1);
                                    this.setData({ member: this.data.member });
                                }
                            }
                            Toast('恢复成功');
                            db.collection('sign-in').add({
                                data: {
                                    userName: e.target.dataset.username,
                                    unionId: e.target.dataset.id,
                                    alliance: app.data.alliance
                                },
                            }).then((res) => {
                            })
                        } else {
                            Toast('恢复失败');
                        }
                    }).catch(err => {
                        Toast('恢复失败请重新恢复');
                    })
                } else {
                    Toast('没有管理员权限');
                }
            })
            .catch(() => {
                // on cancel
            });

    }


})